// pages/filing/filing-form.js
const app = getApp()
const citys = require("../../utils/city_short.js")
Page({

    data: {
        carType: ["蓝牌", "黄牌", "新能源"],
        carAscription: ["公司", "个人"],
        citys,
        addressInArray: [],
        addressInIndex: [0, 0],
        addressOutArray: [],
        addressOutIndex: [0, 0],
    },
    onLoad: function(options) {
        let advance_price = wx.getStorageSync('archives_advance_price')
        //初始化
        var citys = this.data.citys;
        var procince = [];
        var city_code = [];

        for (var i in citys) {
            procince.push(citys[i].city_name)
            if (i == 0) {
                for (var u in citys[i].children) {
                    city_code.push(citys[i].children[u])
                }
            }
        }
        console.log([procince, city_code])
        this.setData(Object.assign({
            advance_price,
            addressInArray: [procince, city_code],
            addressOutArray: [procince, city_code]
        }, app.filingData))
    },
    bindTypeChange(e) {
        this.setData({
            typeIndex: e.detail.value,
            showType: true
        })
    },
    bindAscriptionChange(e) {
        this.setData({
            ascriptionIndex: e.detail.value,
            showAscription: true
        })
    },
    bindAddressInChange(e) {
        this.setData({
            addressInIndex: e.detail.value,
            showAddressIn: true
        })
    },
    bindAddressInColumnChange(e) {
        console.log(e.detail.column, e.detail.value);
        var data = {
            addressInArray: this.data.addressInArray,
            addressInIndex: this.data.addressInIndex
        };
        data.addressInIndex[e.detail.column] = e.detail.value;
        switch (e.detail.column) {
            case 0:
                this.setData({
                    thisProvince: e.detail.value
                })
                //第一栏
                var row = this.getCity(e.detail.value);
                data.addressInArray[1] = row[0];

                //默认选中第一项
                data.addressInIndex[1] = 0;
                break;
        }
        this.setData(data);
    },
    bindAddressOutChange(e) {
        this.setData({
            addressOutIndex: e.detail.value,
            showAddressOut: true
        })
    },
    bindAddressOutColumnChange(e) {
        var data = {
            addressOutArray: this.data.addressOutArray,
            addressOutIndex: this.data.addressOutIndex
        };
        data.addressOutIndex[e.detail.column] = e.detail.value;
        switch (e.detail.column) {
            case 0:
                this.setData({
                    thisProvince: e.detail.value
                })
                //第一栏
                var row = this.getCity(e.detail.value);
                data.addressOutArray[1] = row[0];

                //默认选中第一项
                data.addressOutIndex[1] = 0;
                break;
        }
        this.setData(data);
    },
    getCity: function(x, y = 999, ) {
        var citys = this.data.citys;
        var city_code = [];
        for (var i in citys) {
            if (i == x) {
                for (var u in citys[i].children) {
                    city_code.push(citys[i].children[u])

                }
                break;
            }
        }
        return [city_code];

    },
    submit(evt) {
        let {
            carType = [], carAscription = [], addressInArray = [], addressOutArray = [], addressInIndex = [0, 0], addressOutIndex = [0, 0]
        } = this.data
        let {
            value
        } = evt.detail
        if (!this.data.showType) {
            app.wxToast.warn('请选择车辆类型')
            return
        }
        if (!this.data.showAscription) {
            app.wxToast.warn('请选择车辆所属')
            return
        }
        if (!this.data.showAddressIn) {
            app.wxToast.warn('请选择转入地')
            return
        }
        if (!this.data.showAddressOut) {
            app.wxToast.warn('请选择转出地')
            return
        }
        if (!value.name) {
            app.wxToast.warn('请填写姓名')
            return
        }
        if (!value.phone) {
            app.wxToast.warn('请填写手机号')
            return
        }
        var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
        if (!reg.test(value.phone)) {
            app.wxToast.warn('手机号不正确')
            return
        }
        if (this.data.loading) {
            return
        }

        var that = this

        function f() {
            wx.showLoading({
                title: '请稍等...',
            })
            that.setData({
                loading: true
            })

            app.http.post('rest/archives/create', {
                data: {
                    type: that.data.carValue,
                    carType: carType[that.data.typeIndex],
                    carAscription: carAscription[that.data.ascriptionIndex],
                    addressIn: addressInArray[1][addressInIndex[1]],
                    addressOut: addressOutArray[1][addressOutIndex[1]],
                    entrust: that.data.entrustValue,
                    person: value.name,
                    phone: value.phone
                }
            }).then(res => {
                wx.hideLoading()
                that.setData({
                    loading: false
                })
                if (res.code == "200") {
                    wx.navigateTo({
                        url: './detail?id=' + res.content.id,
                    })
                }
            })
        }

        var advance_price = (this.data.advance_price / 100).toFixed(2)
        if (this.data.carValue == "无车") {
            wx.showModal({
                title: '温馨提示',
                content: '需提前支付' + advance_price + '元诚意金',
                confirmColor: "#2691df",
                success: res => {
                    res.confirm && f()
                    res.cancel && wx.showToast({
                        icon: "none",
                        title: '支付已取消',
                    })
                }
            })
            return
        }

        f()

    }
})